package com.cy.cy2025.activity

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import com.cy.cy2025.databinding.ActivityMainBinding
import com.cy.cy2025.adapter.MainAdapter
import com.cy.cy2025.vm.MainVm

class MainActivity : AppCompatActivity() {

    val vm by lazy { ViewModelProvider(this)[MainVm::class.java] }

    val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    val adapter = MainAdapter()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initView()
        initObserver()
        initData()
    }

    private fun initView() {
        binding.rv.adapter = adapter
        adapter.setOnItemClickListener { adapter, view, position ->
            adapter.getItem(position)?.let {
                view.context.startActivity(Intent(view.context, it.clazz))
            }
        }
    }

    private fun initObserver() {
        vm.dataList.observe(this) {
            adapter.submitList(it)
        }
    }

    private fun initData() {
        vm.loadData()
    }
}